<?php

import("ORG.Util.Function");
import("@.Org.Utils");


class BookingModel extends Model{


	/**
	 * 获取订购信息
	 *
	 * @access  public
	 *
	 * @return array
	 */
	function get_sql_booking_list(){
		$CommonModel	=	new CommonModel();
	    /* 查询条件 */
	    $filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
	    if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1){
	        $filter['keywords'] = $filter['keywords'];
	    }
	    $filter['dispose']    = empty($_REQUEST['dispose']) ? 0 : intval($_REQUEST['dispose']);
	    $filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'sort_order' : trim($_REQUEST['sort_by']);
	    $filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
	
	    $where = (!empty($_REQUEST['keywords'])) ? " AND g.goods_name LIKE '%" . Utils::mysql_like_quote($filter['keywords']) . "%' " : '';
	    $where .= (!empty($_REQUEST['dispose'])) ? " AND bg.is_dispose = '$filter[dispose]' " : '';
		
	    /* 获取活动数据 */
	    $sql = 'SELECT bg.rec_id, bg.link_man, g.goods_id, g.goods_name, bg.goods_number, bg.booking_time, bg.is_dispose '.
	            'FROM ' .'ybr_booking_goods'. ' AS bg, ' .'ybr_goods'. ' AS g '.
	            "WHERE bg.goods_id = g.goods_id $where " .
	            "ORDER BY $filter[sort_by] $filter[sort_order] ";
	    return $sql;
	}
	
	/**
	 * 获得缺货登记的详细信息
	 *
	 * @param   integer     $id
	 *
	 * @return  array
	 */
	function get_booking_info($id){
		$CommonModel	=	new CommonModel();		
	    $sql 			=	"SELECT bg.rec_id, bg.user_id, IFNULL(u.user_name, '$_LANG[guest_user]') AS user_name, ".
	                		"bg.link_man, g.goods_name, bg.goods_id, bg.goods_number, ".
	                		"bg.booking_time, bg.goods_desc,bg.dispose_user, bg.dispose_time, bg.email, ".
			                "bg.tel, bg.dispose_note ,bg.dispose_user, bg.dispose_time,bg.is_dispose  ".
	            			"FROM " . 'ybr_booking_goods'." AS bg ".
	            			"LEFT JOIN " . 'ybr_goods' . " AS g ON g.goods_id=bg.goods_id ".
	            			"LEFT JOIN " . 'ybr_users' . " AS u ON u.user_id=bg.user_id ".
	            			"WHERE bg.rec_id ='$id'";
	    
	    $res 			=	$CommonModel->GetRow($sql);	
	    /* 格式化时间 */
	    $res['booking_time'] 		= 	local_date($GLOBALS['_CFG']['time_format'],$res['booking_time']);
	    if (!empty($res['dispose_time'])){
	        $res['dispose_time'] 	= 	local_date($GLOBALS['_CFG']['time_format'],$res['dispose_time']);
	    }	
	    return $res;
	}
		
	

	
}